﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace CommonLib
{
    public class Fx
    {
        /// <summary>
        /// 直角坐标系 点斜式 :y=kx+b
        /// halcon坐标系 点斜式: y=-kx+b
        /// </summary>
        double k=0, b=0;
        public Fx(double k,double b)
        {
            this.k = k;
            this.b = b;
        }


        public Fx(double x,double y, double k)
        {
            this.k = k;
            this.b = (-k * x + y);
        }

        public Fx(double x1,double y1,double x2,double y2)
        {
            k =(y2 - y1) / (x2 - x1);
            b = y2 - k * x2;
        }

  
        public double GenY(double x)
        {
            return k * x + b;
        }


        public void GetVerticalPoint(double x,double y,out double rsX,out double rsY)
        {
            rsX = (y * k - b * k + x) / (1 + k * k);
            rsY = k * rsX + b;
        }



        /// <summary>
        /// 点距离公式计算(单点)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public double GenDist(double x,double y)
        {
            double rs=0;
            rs = Math.Abs(k * x - y + b)/Math.Sqrt(Math.Pow(k, 2)+1);
            return rs;
        }

        public double K { get { return k; } set { k = value; } }

        /// <summary>
        /// 点距离公式计算(多点)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public List<double> GenDist(double []xs, double []ys)
        {
            List<double> rs = new List<double>();
            for (int i = 0; i < xs.Length; i++)
            {
                double dist = GenDist(xs[i], ys[i]);
                rs.Add(dist);
            }
            return rs;
        }
    }
}
